<?php

/**
 * Description of Utilitarios_Usuario
 *
 * @author Jean Brito
 */
class Application_Model_Utilitarios_Usuario extends Application_Model_Base {

    protected $_table = 'utilitario_usuario';
    protected $_fields = Array(
        'id' => Array(
            'name' => 'id',
            'type' => 'identifier',
            'label' => 'Id',
        ),
        'nome' => Array(
            'name' => 'nome',
            'type' => 'name',
            'label' => 'Nome',
        ),
        'login' => Array(
            'name' => 'login',
            'type' => 'login',
            'label' => 'Login',
        ),
        'senha' => Array(
            'name' => 'senha',
            'type' => 'password',
            'label' => 'Senha',
        ),
        'email' => Array(
            'name' => 'email',
            'type' => 'email',
            'label' => 'Email',
        ),
        'situacao' => Array(
            'name' => 'situacao',
            'type' => 'select',
            'label' => 'Situação',
            'options' => array('A' => 'Ativo', 'B' => 'Bloqueado')
        ),
    );

    public function init() {

        $this->_views1 = Array(
            'index' => Array(
                
            )
        );

        // new Zend_Json_Expr("function(config){return Ext.create('AgenteNET.Utilitarios.Controle',config);}")

        parent::init();
    }

    /*
      protected $_portable='USUUSU';
      protected $_portableField=Array(
      'id'=>'USUCOD',
      'nome'=>'USUNOM',
      'login'=>'USUIDE',
      'senha'=>'USUSEN',
      'email'=>'USUEMA'
      );

      public function index($filter = '', $start = '', $limit = '', $sort = '') {
      $client = new Zend_Rest_Client("http://" . $this->getHost());
      $query["query"] = "select USUCOD as id,USUNOM as nome,USUEMA as email,USUIDE as login,'A' as situacao from USUUSU";
      //$query["query"] = "select SINCOD as id,SINNOM as nome,SINEMA as email,SINMAT as login from USUSIN";
      //$query["query"] = "select SLTCOD as id,SLTNOM as nome,SLTEMA as email,SLTCOD as login from USUSLT";

      $query['filter'] = $filter;
      $query['start'] = $start;
      $query['limit'] = $limit;
      $query['sort'] = $sort;


      $restResult = $client->restGet("/sistemas/agentenet.php", $query);
      //print_r($restResult->getRawBody());exit;
      try {
      $data = Zend_Json::decode($restResult->getRawBody());
      unset($data['data'][count($data['data']) - 1]);
      } catch (Exception $e) {
      //echo $e->getMessage();
      $data = array('success' => false, "message" => $e->getMessage()); //$restResult->getBody();
      }
      return $data;
      }

      public function get($id) {
      $client = new Zend_Rest_Client("http://" . $this->getHost());
      $query["query"] = "select USUCOD as id,USUNOM as nome,USUEMA as email,USUIDE as login,USUSIT as situacao from USUUSU where USUCOD='$id'";
      $restResult = $client->restGet("/sistemas/agentenet.php", $query);
      try {
      $data = Zend_Json::decode($restResult->getRawBody());
      unset($data['data'][count($data['data']) - 1]);
      } catch (Exception $e) {
      $data = $restResult->getBody();
      }
      return $data;
      }

      public function post($data) {
      $client = new Zend_Rest_Client("http://" . $this->getHost());
      $query["query"] = "insert into USUUSU (USUCOD,USUNOM,USUIDE,USUEMA,USUSIT)values((select max(SINCOD) from USUSIN)+1,'" . $data['nome'] . "','" . $data['login'] . "','" . $data['email'] . "','" . $data['situacao'] . "')";
      $restResult = $client->restGet("/sistemas/agentenet.php", $query);
      try {
      $data = Zend_Json::decode($restResult->getRawBody());
      unset($data['data'][count($data['data']) - 1]);
      return array('success' => true);
      } catch (Exception $e) {
      $data = $restResult->getBody();
      }
      return $data;
      }

      public function delete($id) {
      $client = new Zend_Rest_Client("http://" . $this->getHost());
      $query["query"] = "delete from USUUSU where(USUCOD='$id')";
      $restResult = $client->restGet("/sistemas/agentenet.php", $query);
      try {
      $data = Zend_Json::decode($restResult->getRawBody());
      unset($data['data'][count($data['data']) - 1]);
      return array('success' => true);
      } catch (Exception $e) {
      $data = $restResult->getBody();
      }
      return $data;
      }

      public function put($id, $data) {
      $client = new Zend_Rest_Client("http://" . $this->getHost());
      $query["query"] = "update USUUSU set USUNOM='" . $data['nome'] . "',USUIDE='" . $data['login'] . "',USUEMA='" . $data['email'] . "',USUSIT='" . $data['situacao'] . "' where(USUCOD='$id')";
      $restResult = $client->restGet("/sistemas/agentenet.php", $query);
      try {
      $data = Zend_Json::decode($restResult->getRawBody());
      unset($data['data'][count($data['data']) - 1]);
      return array('success' => true);
      } catch (Exception $e) {
      $data = $restResult->getBody();
      }
      return $data;
      }
     */
}
